//
//  ViewController.m
//  testApp
//
//  Created by HN on 2022/1/6.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>

@interface ViewController ()<WKNavigationDelegate>

@property (strong, nonatomic) WKWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.webView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:self.webView];
    
    //获取到需要加载的HTML
    NSString *HtmlString = [self getHTMLString];

    NSString *style = @"<style>*{padding:0;margin:0}img{width:100% !important;height:auto;vertical-align: bottom;}</style>";
    NSString *str1 = @"<img src=\"https:\/\/app.tommoo.com\/upload\/image\/20220106\/20220106081211_97668.jpg\" align=\"left\" alt=\"\" border=\"0\" \/>";
    NSString *str = @"<img src=\"https:\/\/app.tommoo.com\/upload\/image\/20211120\/20211120112205_73851.jpg\" alt=\"\" border=\"0\" \/><img src=\"https:\/\/app.tommoo.com\/upload\/image\/20211120\/20211120112215_81781.jpg\" alt=\"\" border=\"0\" \/><img src=\"https:\/\/app.tommoo.com\/upload\/image\/20211120\/20211120112222_78367.jpg\" alt=\"\" border=\"0\" \/><img src=\"https:\/\/app.tommoo.com\/upload\/image\/20211120\/20211120112230_72162.jpg\" alt=\"\" border=\"0\" \/>";
    
    NSString *strResult = [NSString stringWithFormat:@"%@%@", style, str1];
    //图片懒加载处理
    [self lazyloadHTMLImagesWithHTMLString:strResult complete:^(NSString *tempHtml, NSURL *baseURL) {
        //加载html
        [self.webView loadHTMLString:tempHtml baseURL:baseURL];
    }];

}
/*
 {
     "code": 0,
     "msg": "\u6210\u529f",
     "count": 0,
     "data": {
         "goods_id": 14,
         "goods_class_id": 3,
         "goods_name": "60W light",
         "goods_img": "https:\/\/app.tommoo.com\/upload\/20220106\/2946d0c3aa2f21c85a2a142ee351d684.jpg",
         "code": "",
         "price": "70.00",
         "original_price": "99.99",
         "discount": 22,
         "num": 0,
         "status": "0",
         "url": "https:\/\/www.amazon.com\/dp\/B08B557W5D",
         "bluetooth": null,
         "content": "<img src=\"https:\/\/app.tommoo.com\/upload\/image\/20220106\/20220106081211_97668.jpg\" align=\"left\" alt=\"\" border=\"0\" \/>",
         "city_id": null,
         "class_status": "0",
         "status_w": "1",
         "goods_name2": null
     }
 }
 */
- (NSString*)getHTMLString {
    NSString *tempPath = [[NSBundle mainBundle]pathForResource:@"index" ofType:@"html"];
    NSString *HtmlString = [NSString stringWithContentsOfFile:tempPath encoding:NSUTF8StringEncoding error:nil];
    return HtmlString;
}


///实现html图片懒加载
- (void)lazyloadHTMLImagesWithHTMLString:(NSString *)htmlString complete:(void(^)(NSString *tempHtml, NSURL *baseURL))complete{
    //替换标签
    NSString *originalStr = [htmlString stringByReplacingOccurrencesOfString:@"src" withString:@"data-original"];
    //获取temp文件的路径
    NSString *tempPath = [[NSBundle mainBundle]pathForResource:@"temp" ofType:@"html"];
    //加载temp内容为字符串
    NSString *tempHtml = [NSString stringWithContentsOfFile:tempPath encoding:NSUTF8StringEncoding error:nil];
    //替换temp内的占位符{{Content_holder}}为需要加载的HTML代码
    tempHtml = [tempHtml stringByReplacingOccurrencesOfString:@"{{Content_holder}}" withString:originalStr];
    //LazyloadImage目录下的js文件在根路径，因此需要在加载HTMLString时指定根路径
    NSString *basePath = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:basePath];
    
    if (complete) {
        complete(tempHtml,baseURL);
    }
}


#pragma mark - webview加载 delegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
//    self.isLoaded = YES;
    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error){
        CGFloat height = [result floatValue];
        NSLog(@"xwh: %lf", height);
    }];
}



- (WKWebView *)webView
{
    if (!_webView) {
        _webView = [[WKWebView alloc]init];
        _webView.allowsBackForwardNavigationGestures = YES;
        _webView.navigationDelegate = self;
//        _webView.scrollView.scrollEnabled = NO;
        // KVO
//        [_webView addObserver:self
//                   forKeyPath:@"estimatedProgress"
//                      options:NSKeyValueObservingOptionNew
//                      context:nil];
//        [_webView addObserver:self forKeyPath:@"canGoBack"
//                      options:NSKeyValueObservingOptionNew
//                      context:nil];
        // javascript注入
//        [self injectionJS:_webView.configuration];
    }
    return _webView;
}
@end
